package cn.edu.zafu.bigdata.x.zcy_0217;

import java.util.Scanner;
import java.sql.SQLException;


public class StudentInfoSystem {
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        while (true) {
            System.out.println("学生信息管理系统");
            System.out.println("1. 添加学生信息");
            System.out.println("2. 检索学生信息");
            System.out.println("3. 更新学生信息");
            System.out.println("4. 删除学生信息");
            System.out.println("5. 退出程序");
            System.out.print("请输入操作编号 (1-5): ");
            String choice = scanner.nextLine();

            switch (choice) {
                case "1":
                    addStudent();
                    break;
                case "2":
                    searchStudent();
                    break;
                case "3":
                    updateStudent();
                    break;
                case "4":
                    deleteStudent();
                    break;
                case "5":
                    System.out.println("退出程序。");
                    System.exit(0);
                    break;
                default:
                    System.out.println("无效的输入，请重新输入。");
                    break;
            }
        }
    }

    private static void addStudent() {
        System.out.print("请输入学号: ");
        String id = scanner.nextLine();
        System.out.print("请输入姓名: ");
        String name = scanner.nextLine();
        System.out.print("请输入电话: ");
        String phone = scanner.nextLine();

        try {
            if (DatabaseManager.getStudent(id) != null) {
                System.out.println("学号重复，请重新输入。");
            } else {
                DatabaseManager.addStudent(new Student(id, name, phone));
                System.out.println("学生信息添加成功。");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private static void searchStudent() {
        System.out.print("请输入学号进行检索: ");
        String id = scanner.nextLine();
        while (!id.equalsIgnoreCase("QUIT")) {
            try {
                Student student = DatabaseManager.getStudent(id);
                if (student != null) {
                    System.out.println(student);
                } else {
                    System.out.println("未找到该学生。");
                }
                System.out.print("请输入学号进行检索或输入'QUIT'退出: ");
                id = scanner.nextLine();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    private static void updateStudent() {
        System.out.print("请输入学号进行更新: ");
        String id = scanner.nextLine();
        while (!id.equalsIgnoreCase("QUIT")) {
            try {
                Student student = DatabaseManager.getStudent(id);
                if (student != null) {
                    System.out.print("请输入新的姓名: ");
                    String name = scanner.nextLine();
                    System.out.print("请输入新的电话: ");
                    String phone = scanner.nextLine();
                    student.setName(name);
                    student.setPhone(phone);
                    DatabaseManager.updateStudent(student);
                    System.out.println("学生信息更新成功。");
                } else {
                    System.out.println("未找到该学生。");
                }
                System.out.print("请输入学号进行更新或输入'QUIT'退出: ");
                id = scanner.nextLine();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    private static void deleteStudent() {
        System.out.print("请输入学号进行删除: ");
        String id = scanner.nextLine();
        while (!id.equalsIgnoreCase("QUIT")) {
            try {
                Student student = DatabaseManager.getStudent(id);
                if (student != null) {
                    DatabaseManager.deleteStudent(id);
                    System.out.println("学生信息删除成功。");
                } else {
                    System.out.println("未找到该学生。");
                }
                System.out.print("请输入学号进行删除或输入'QUIT'退出: ");
                id = scanner.nextLine();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

